import 'package:flutter/material.dart';
import 'package:p_location/alarm/alarm_manage_tab.dart';
import 'package:p_location/home/tab/test_desk_tab.dart';
import 'package:p_location/map/jaguar_demo.dart';
import 'package:p_location/map/ui/page_person_trace.dart';
import 'package:p_location/user/obj/user_info.dart';
import 'package:p_location/utils/net/trace_module_util.dart';
import 'package:p_location/utils/string_util.dart';

import 'navigation_icon_view.dart';

class Index extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _IndexState();
  }
}

class _IndexState extends State<Index> with TickerProviderStateMixin {
  int _currentIndex = 0;
  List<NavigationIconView> _navigationIconViews;
  List<StatefulWidget> _pageList;
  StatefulWidget _currentPage;

  void _rebuild() {
    setState(() {});
  }

  @override
  void initState() {
    super.initState();
    //初始化导航图标
    _navigationIconViews = <NavigationIconView>[
      NavigationIconView(
          icon: new Icon(Icons.home),
          title: new Text(
            '首页',
            style: TextStyle(fontSize: 12),
          ),
          vsync: this),
      NavigationIconView(
          icon: new Icon(Icons.place),
          title: new Text(
            '定位',
            style: TextStyle(fontSize: 12),
          ),
          vsync: this),
//      NavigationIconView(
//          icon: new Icon(Icons.notifications),
//          title: new Text(
//            '消息',
//            style: TextStyle(fontSize: 12),
//          ),
//          vsync: this),
      NavigationIconView(
          icon: new Icon(Icons.merge_type),
          title: new Text(
            '轨迹',
            style: TextStyle(fontSize: 12),
          ),
          vsync: this),
      NavigationIconView(
          icon: new Icon(Icons.notifications_active),
          title: new Text(
            '报警',
            style: TextStyle(fontSize: 12),
          ),
          vsync: this),
    ];

    for (NavigationIconView view in _navigationIconViews) {
      view.controller.addListener(_rebuild);
    }

    _pageList = <StatefulWidget>[
      new TestDeskTab(),
      new GisMapPage(),
      PagePersonTrace(),
//      new DepartmentStaffInfoListPage(type:'4'),
//      new MessageTab(),
//      new GisMapPage(),
      EmergencyManageTab(isHiddenAppbar: true,)
    ];

    _currentPage = _pageList[_currentIndex];
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    print('state$state');
    setState(() {});
  }

  @override
  void dispose() {
    for (NavigationIconView view in _navigationIconViews) {
      view.controller.dispose();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final BottomNavigationBar bottomNavigationBar = new BottomNavigationBar(
      items: _navigationIconViews
          .map((NavigationIconView navigationIconView) =>
              navigationIconView.item)
          .toList(),
      currentIndex: _currentIndex,
      type: BottomNavigationBarType.fixed,
      onTap: (int index) {
        setState(() {
          if (index == 1) { //跳转到定位
            if (UserInfo.mLoginRes != null && UserInfo.mLoginRes.data != null) {
              if (StringUtil.isNotEmpty(UserInfo.mLoginRes.data.accountName)) {
                TraceModuleUtil(UserInfo.mLoginRes.data.accountName, "定位")
                    .requestTrace(context);
              }
            }
          }
//          if (index == 2 || index == 3) {
//            NavigatorUtil.push(context, GisMapPage());
//          } else {
          _navigationIconViews[_currentIndex].controller.reverse();
          _currentIndex = index;
          _navigationIconViews[_currentIndex].controller.forward();
          _currentPage = _pageList[_currentIndex];
//          }
        });
      },
    );

    return Scaffold(
      body: new Center(child: _currentPage),
      bottomNavigationBar: bottomNavigationBar,
    );
  }
}
